package cn.damai.order.service.impl;

import cn.damai.model.Image;
import cn.damai.model.SchedulerSeat;
import cn.damai.order.mapper.OrderMapper;
import cn.damai.model.Order;
import cn.damai.order.model.OrderDTO;
import cn.damai.order.service.OrderService;
import cn.damai.util.DubboUtil;
import cn.damai.util.RandomNumberGenerator;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 订单相关实现类
 */
@Component
@Service(version = DubboUtil.VRESION,group = DubboUtil.GROUP)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

//测试区域start=================================================
    @Override
    public List<Order> queryAll() {
        return orderMapper.queryAll();
    }
//测试区域end=================================================

//开发区域==================================================
    @Override
    public Order confirmOrder(OrderDTO orderDTO) {
//        设置一些必须的值
        orderDTO.setOrderNo(new Date().getTime()+"");
        orderDTO.setAliTradeNo(RandomNumberGenerator.generateNumber());
        orderDTO.setOrderType(0);
        orderDTO.setPayType("2");
        orderDTO.setSourceType(0);
        orderDTO.setCreatedTime(new Date());

        orderMapper.confirmOrder(orderDTO);
        Order order = orderMapper.queryOrderByOrderNo(orderDTO.getOrderNo());
        orderDTO.setId(order.getId());

        orderMapper.addOrderLinkUser(orderDTO);
        orderMapper.updateSchedulerSeat(orderDTO);
//          定时器
        timeoutPayHandle(orderDTO.getOrderNo());

        return order;
    }

//    下单超时交易取消,10分钟(1000 * 60 * 10)
    private void timeoutPayHandle(String orderNo) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Order order = orderMapper.queryOrderByOrderNo(orderNo);
                if (order.getOrderType() == 0){
                    orderMapper.updateOrderType(order.getOrderNo());
                    orderMapper.updateSchedulerSeatStatus(order.getOrderNo());
                }
            }
        },1000*60*3);
    }

    @Override
    public List<OrderDTO> orderListByUserId(Long userId) {
        List<OrderDTO> orderDTOS = orderMapper.orderListByUserId(userId);
//          放个默认图片
        Image image = new Image();
        image.setImgUrl("/damai/image/failPicture.jpg");

        for(int i = 0;i<orderDTOS.size();i++){
            if(orderDTOS.get(i).getImage() == null){
                orderDTOS.get(i).setImage(image);
            }
        }

        return orderDTOS;
    }

    @Override
    public OrderDTO orderDetailByOrderNo(String orderNo) {
        OrderDTO orderDTO = orderMapper.orderDetailByOrderNo(orderNo);

        Image image = new Image();
        image.setImgUrl("/damai/image/failPicture.jpg");

        if(orderDTO.getImage() == null){
            orderDTO.setImage(image);
        }

        return orderDTO;
    }

    @Override
    public OrderDTO orderReady(Order order, Integer areaLevel) {

        OrderDTO orderDTO = new OrderDTO();
//        判断有座或无座的票都售完没
        List<SchedulerSeat> schedulerSeats = orderMapper.listSeatBySchedulerAndAreaLevel(order.getSchedulerId(),areaLevel);

        SchedulerSeat schedulerSeat = new SchedulerSeat();

        for(int i = 0;i < schedulerSeats.size();i++){
//            0:未出售 1:锁定待付款 2:已出售
            if(schedulerSeats.get(i).getSaleStatus() == 0){
                schedulerSeat = schedulerSeats.get(i);
                break;
            }
//          票已售完
            if(i == schedulerSeats.size()-1){
                return null;
            }
        }

        orderDTO = orderMapper.listItemInfo(order, areaLevel);
        orderDTO.setSchedulerSeat(schedulerSeat);
        orderDTO.setUserId(order.getUserId());
        orderDTO.setTotalCount(order.getTotalCount());
//        价格计算
        BigDecimal bd = BigDecimal.valueOf(orderDTO.getTotalCount());
        orderDTO.setTotalAmount(bd.multiply(orderDTO.getSchedulerSeatPrice().getPrice()));

        return orderDTO;
    }

    @Override
    public Order queryOrderByOrderNo(String orderNo) {
        return orderMapper.queryOrderByOrderNo(orderNo);
    }

    @Override
    public void updateOrder(Order order) {
        orderMapper.updateOrder(order);
        orderMapper.saledSchedulerSeat(order);
        orderMapper.addTrade(order);
    }

    @Override
    public Order orderInfoByOrderNo(String orderNo) {
        return queryOrderByOrderNo(orderNo);
    }

    @Override
    public boolean payCancel(String orderNo) {
        Order order = queryOrderByOrderNo(orderNo);

        if(order == null){
            return true;
        }else {
            orderMapper.updateOrderType(orderNo);
            orderMapper.updateSchedulerSeatStatus(orderNo);
            return false;
        }
    }
}
